//
//  Precedence.swift
//  PrattParser
//
//  Created by 神经骚栋 on 2023/9/13.
//

import Foundation


enum Precedence : Int {
    case LOWEST         = 0
    
    case EQUALS         = 1
    
    case LESS_GREATER   = 2
    
    case SUM            = 3
    
    case PRODUCT        = 4
    
    case POWER          = 5
    
    case PREFIX         = 6
    
    case CALL           = 7
    
    static var precedences: [TokenType : Precedence] = [:]

    static func getPrecedence(_ type: TokenType?) -> Precedence {
        precedences.updateValue(Precedence.SUM, forKey: TokenType.PLUS)
        precedences.updateValue(Precedence.SUM, forKey: TokenType.MINUS)
        precedences.updateValue(Precedence.PRODUCT, forKey: TokenType.SLASH)
        precedences.updateValue(Precedence.PRODUCT, forKey: TokenType.ASTERISK)
        precedences.updateValue(Precedence.POWER, forKey: TokenType.HAT)
        precedences.updateValue(Precedence.CALL, forKey: TokenType.LPAREN)
        
        return precedences[type ?? TokenType.None] ?? Precedence.LOWEST;
    }
    
}
